using NUnit.Framework;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class BeginBlockProductionSpecification {
        [Test]
        public void ShouldParseOpenBracket() {
            var source = " {";

            var result = BeginBlockProduction.CreateTerminal ().Parse (source);

            Assert.IsNotNull (result);
            Assert.AreEqual ("{", ((KeywordTerm) result.Term).Name);
            Assert.AreEqual (1, result.Context.BracketDepth);
            Assert.AreEqual (2, result.Index);
            Assert.AreEqual (new SourceInformation (null, 1, 1), result.Term.SourceInformation);
        }

        [Test]
        public void ShouldParseIndentedNewLine() {
            var source = " \n    id";

            var result = BeginBlockProduction.CreateTerminal ().Parse (source);

            Assert.IsNotNull (result);
            Assert.AreEqual ("{", ((KeywordTerm) result.Term).Name);
            Assert.AreEqual ("    ", result.Context.LastIndent);
            Assert.AreEqual (6, result.Index);
            Assert.AreEqual (new SourceInformation (null, 1, 5), result.Term.SourceInformation);
        }

        [Test]
        public void ShouldNotParseIndentedNewLineIfBracketDepthIsGreaterThanZero() {
            var source = " \n    id";

            var result = BeginBlockProduction.CreateTerminal ().Parse (source, 0, new ParseContext () {BracketDepth = 1});

            Assert.IsNull (result);
        }

        [Test]
        public void ShouldParseOpenBraceOnIndentedNewLineIgnoreIndent() {
            var source = " \n    {";

            var result = BeginBlockProduction.CreateTerminal ().Parse (source);

            Assert.IsNotNull (result);
            Assert.AreEqual ("{", ((KeywordTerm) result.Term).Name);
            Assert.AreEqual ("", result.Context.LastIndent);
            Assert.AreEqual (1, result.Context.BracketDepth);
            Assert.AreEqual (7, result.Index);
            Assert.AreEqual (new SourceInformation (null, 6, 1), result.Term.SourceInformation);
        }

        [Test]
        public void ShouldParseOpenBraceAndSkipNewLine() {
            var source = " {\n    id";

            var result = BeginBlockProduction.CreateTerminal ().Parse (source);

            Assert.IsNotNull (result);
            Assert.AreEqual ("{", ((KeywordTerm) result.Term).Name);
            Assert.AreEqual ("    ", result.Context.LastIndent);
            Assert.AreEqual (1, result.Context.BracketDepth);
            Assert.AreEqual (7, result.Index);
            Assert.AreEqual (new SourceInformation (null, 1, 1), result.Term.SourceInformation);
        }

        [Test]
        public void ShouldNotParseAnythingElse() {
            var source = " id";

            var result = BeginBlockProduction.CreateTerminal ().Parse (source);

            Assert.IsNull (result);
        }
    }
}